package com.bnz.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class FirstInterceptor implements HandlerInterceptor {

	//1. 调用处理器方法前调用
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		System.out.println("FirstInterceptor--->preHandle()");
		return true;
	}

	//2. 在执行完处理器方法后调用,此时Model并未渲染到页面
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		if(modelAndView != null) {
			String viewName = modelAndView.getViewName();
			System.out.println("viewName = " + viewName);
		}

		System.out.println("FirstInterceptor--->postHandle()");
	}

	//3. 在页面渲染完成后，做统一收尾的处理工作
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		System.out.println("FirstInterceptor--->afterCompletion()");
	}
}
